<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="[currentRoute]"
        @menu-item-click="doMenuClick"
      >
        <!-- 图标  -->
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/logo.svg" alt="" />
            <div class="title">小韩-OJ</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
    </a-col>
  </a-row>
</template>
<script setup lang="ts">
import { routes } from "@/router/routers";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const store = useStore();
const router = useRouter();
const loginUser = computed(() => {
  return store.state.user?.loginUser;
});
// 默认主页
const currentRoute = ref("/");

// 监听路由变化更新选中状态
router.afterEach((to) => {
  currentRoute.value = to.path;
});
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    if (
      !checkAccess(loginUser.value, item?.meta?.access as string | string[])
    ) {
      return false;
    }
    return true;
  });
});

const doMenuClick = (key: string) => {
  router.push(key);
};

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "hc",
//     userRole: ACCESS_ENUM.USER,
//   });
// }, 3000);
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  font-size: 24px;
  color: #4dc373;
  margin-left: 10px;
}

.logo {
  height: 48px;
}
</style>
